<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/color.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.easyui.min.js"></script>
</head>
<body>
	<table id="car_dg" title="品牌列表" class="easyui-datagrid"
		style="width: 100%; height: 480px"
		url="${param.request.contextPath}/car/list" toolbar="#car_toolbar"
		pagination="true" rownumbers="true" fitColumns="true"
		singleSelect="true">
		<thead>
			<tr>
				<th field="cb" checkbox="true" align="center"></th>
				<!--  field字段需要与返回数据的属性名一致 -->
				<th field="carId" align="center">汽车编号</th>
				<th field="carName" width="50">汽车名称</th>
				<th field="brand" formatter="brandFormatter" width="50">品牌</th>
				<th field="type" formatter="typeFormatter" width="50">车型</th>
				<th field="provider" formatter="providerFormatter" width="50">供应商</th>
				<th field="carPrice" width="50">价格</th>
				<th field="carPhoto" formatter="imageFormatter" align="center"
					width="50" height="45">参考图片</th>
				<th field="salesStatus" width="50">在售状态</th>
			</tr>
		</thead>
	</table>

	<div id="car_toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="newCar()">新增</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editCar()">修改</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyCar()">删除</a>
	</div>

	<div id="car_dlg" class="easyui-dialog"
		style="width: 700px; height: 480px; padding: 10px 20px" closed="true"
		buttons="#car_dlg-buttons">
		<div class="ftitle">汽车信息管理</div>
		<form id="car_fm" enctype="multipart/form-data" method="post">
			<div class="fitem">
				<label>汽车名称:</label> <input name="carName"
					class="easyui-validatebox" required="true">
			</div>
			<div class="fitem">
				<label>汽车品牌:</label> <input id="brandName" name="brand.brandId"
					class="easyui-combobox" 
					data-options="valueField:'brandId',textField:'brandName',url:'${param.request.contextPath}/brand/findall'">
			</div>
			<div class="fitem">
				<label>汽车车型:</label> <input id="typeName" name="type.typeId"
					class="easyui-combobox" 
					data-options="valueField:'typeId',textField:'typeName',url:'${param.request.contextPath}/typr/findall'">
			</div>
			<div class="fitem">
				<label>&nbsp;&nbsp;供应商:</label> <input id="providerName"
					name="provider.providerId" class="easyui-combobox"
					data-options="valueField:'providerId',textField:'providerName',url:'${param.request.contextPath}/provider/findall'">

			</div>
			<div class="fitem">
				<label>销售价格:</label> <input name="carPrice" class="easyui-textbox">
			</div>
			<div class="fitem">
				<label>参考图片:</label> <input name="imageFile" class="easyui-filebox">
			</div>

			<div class="fitem">
				<label>在售状态:</label> <select class="easyui-combobox"
					name="salesStatus" style="width: 150px" required="true">
					<option value="在售">在售</option>
					<option value="下架">下架</option>
				</select>
			</div>

		</form>
	</div>

	<div id="car_dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
			onclick="save()">保存</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#car_dlg').dialog('close')">取消</a>
	</div>


	<script type="text/javascript">
		function brandFormatter(value, row, index) {
			return row.brand.brandName;
		}

		function typeFormatter(value, row, index) {
			return row.type.typeName;
		}

		function providerFormatter(value, row, index) {
			return row.provider.providerName;
		}
		
		function loadBrand(brand) {
			var data = $("#brandName").combobox("getData");
			if (data && data.length > 0) {
				if (brand == 0) {
					$("#brandName").combobox("setValue", data[0].brandId);
				} else {
					$("#brandName").combobox("setValue", brand);
				}
			}
		}
		function loadType(type) {
			var data = $("#typeName").combobox("getData");
			if (data && data.length > 0) {
				if (type == 0) {
					$("#typeName").combobox("setValue", data[0].typeId);
				} else {
					$("#typeName").combobox("setValue", type);
				}
			}
		}

		function loadProvider(provider) {
			var data = $("#providerName").combobox("getData");
			if (data && data.length > 0) {
				if (provider == 0) {
					$("#providerName").combobox("setValue", data[0].providerId);
				} else {
					$("#providerName").combobox("setValue", provider);
				}
			}
		}


		
		function newCar() {
			$('#car_dlg').dialog('open').dialog('setTitle', '新增管理');
			$('#car_fm').form('clear');
			url = '${pageContext.request.contextPath}/car/add';
		}

		function editCar() {
			$('#car_fm').form('clear');
			var row = $('#car_dg').datagrid('getSelected');
			if (row) {
				$('#car_dlg').dialog('open').dialog('setTitle', '修改管理');
				$('#car_fm').form('load', row);
				url = '${pageContext.request.contextPath}/car/edit?carId='
						+ row.carId;
			}
			loadBrand(row.brand.brandId);
			loadType(row.type.typeId);
			loadProvider(row.provider.providerId);
		}

		function save() {
			$('#car_fm').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(result) {
					var result = eval('(' + result + ')');
					if (!result.success) {
						$.messager.show({
							title : 'Error',
							msg : result.errorMsg
						});
					} else {
						$.messager.show({
							title : 'Info',
							msg : result.errorMsg
						});
					}

					$('#car_dlg').dialog('close'); // close the dialog
					$('#car_dg').datagrid('reload'); // reload the user data
				}
			});
		}

		function destroyCar() {
			var row = $('#car_dg').datagrid('getSelected');
			if (row) {
				$.messager
						.confirm(
								'Confirm',
								'确认删除?',
								function(r) {
									if (r) {
										$
												.post(
														'${pageContext.request.contextPath}/car/delete',
														{
															carId : row.carId
														},
														function(result) {
															if (result.success) {
																$('#car_dg')
																		.datagrid(
																				'reload'); // reload the user data
															} else {
																$.messager
																		.show({ // show error message
																			title : 'Error',
																			msg : result.errorMsg
																		});
															}
														}, 'json');
									}
								});
			}
		}

		function imageFormatter(value, row, index) {
			return "<img  width='45' height='45' src='${pageContext.request.contextPath}/"+value+"'>";
		}
	</script>
</body>
</html>